package com.fingersoft.dexload;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import com.fingersoft.im.utils.ApplicationUtils2Kt;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import cn.fingersoft.util.BuildConfigUtil;

import static android.content.Context.MODE_MULTI_PROCESS;

/**
 * @author：jooper Email：jooperge@163.com
 * 描述：dex独立进程异步加载utils
 * 修改历史:
 * <p>
 * 创建于： 2019-07-25
 */
public class DexLoadUtil {

    private static DexLoadUtil activityUtils;
    private Map<String, Activity> activityMap = new HashMap<>();
    private final String KEY_ACTIVITY_DEXPRELOAD = "activity_dexpreload";

    public static DexLoadUtil getInstance() {
        if (activityUtils == null) {
            activityUtils = new DexLoadUtil();
        }
        return activityUtils;
    }

    public void addDexActivity(Activity activity) {
        if (activityMap.get(KEY_ACTIVITY_DEXPRELOAD) == null) {
            activityMap.put(KEY_ACTIVITY_DEXPRELOAD, activity);
        }
    }

    public void finishDexActivity(Context context) {
        Activity activity = activityMap.get(KEY_ACTIVITY_DEXPRELOAD);
        if (activity != null) {
            if (activity.isDestroyed() || activity.isFinishing()) {
                activityMap.remove(KEY_ACTIVITY_DEXPRELOAD);
                return;
            }
            activity.finish();
            activityMap.remove(KEY_ACTIVITY_DEXPRELOAD);
            ApplicationUtils2Kt.killProcess(context,
                BuildConfigUtil.INSTANCE.getString("APPLICATION_ID", "") + ":preloaddex");
        }
    }

    public boolean getPreloadstatus(WeakReference<Context> context) {
        if (null == context || null == context.get()) {
            return false;
        }
        SharedPreferences sp = context.get().getSharedPreferences("multidex", MODE_MULTI_PROCESS);
        return sp.getBoolean("dexoptdone", false);
    }

    public void setPreloadstatus(WeakReference<Context> context, boolean newStatus) {
        if (null == context || null == context.get()) {
            return;
        }
        SharedPreferences sp = context.get().getSharedPreferences("multidex", Context.MODE_MULTI_PROCESS);
        sp.edit().putBoolean("dexoptdone", newStatus).commit();
    }

    public void setIsAppRestartOnce(WeakReference<Context> context, boolean newStatus) {
        if (null == context || null == context.get()) {
            return;
        }
        SharedPreferences sp = context.get().getSharedPreferences("isappfullrestartonce", Context.MODE_MULTI_PROCESS);
        sp.edit().putBoolean("isappfullrestartonce", newStatus).commit();
    }

    public boolean getIsAppRestartOnce(WeakReference<Context> context) {
        if (null == context || null == context.get()) {
            return false;
        }
        SharedPreferences sp = context.get().getSharedPreferences("isappfullrestartonce", MODE_MULTI_PROCESS);
        return sp.getBoolean("isappfullrestartonce", false);
    }

    public void setIfNeedDexPreLoad(WeakReference<Context> context, boolean newStatus) {
        if (null == context || null == context.get()) {
            return;
        }
        SharedPreferences sp = context.get().getSharedPreferences("isappfullrestart", Context.MODE_MULTI_PROCESS);
        sp.edit().putBoolean("isappfullrestart", newStatus).commit();
    }

    public boolean getIfNeedDexPreLoad(WeakReference<Context> context) {
        SharedPreferences sp = context.get().getSharedPreferences("isappfullrestart", MODE_MULTI_PROCESS);
        return sp.getBoolean("isappfullrestart", false);
    }

}
